#include <GL/glut.h>
#include <cmath>
#include <iostream>

/**
 * 重绘窗口大小
 * @param width 窗口宽度
 * @param height 窗口高度
 */
void ChangeSize(GLsizei width, GLsizei height) {
    if (width < 1) width = 1;
    if (height < 1) height = 1;
    std::cout<<width<<" "<<height<<std::endl;
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    if (width <= height)
        glOrtho(-30.0, 30.0, -30.0 * static_cast<GLfloat>(height) / static_cast<GLfloat>(width),
                30.0 * static_cast<GLfloat>(height) / static_cast<GLfloat>(width), -50.0, 50.0);
    else
        glOrtho(-30.0 * static_cast<GLfloat>(width) / static_cast<GLfloat>(height),
                30.0 * static_cast<GLfloat>(width) / static_cast<GLfloat>(height), -30.0, 30.0, -50.0, 50.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

/**
 * 绘制图形
 */
void DrawMyObjects() {
    //画四边形
    glBegin(GL_QUADS);
    glColor3f(0, 0, 1);
    glVertex2f(0, 0);
    glVertex2f(0, 5);
    glVertex2f(5, 5);
    glVertex2f(5, 0);
    glEnd();
    //画三角形
    glBegin(GL_TRIANGLES);
    glColor3f(1, 0, 0);
    glVertex2f(-2, -2);
    glVertex2f(-2, 2);
    glVertex2f(2, -2);
    glEnd();
    //画线
    glBegin(GL_LINES);
    glColor3f(1, 0, 0);
    glVertex2f(-10, -10);
    glVertex2f(-10, 0);
    glEnd();
    glBegin(GL_POINTS);
    glColor3f(1, 1, 0);
    for (int i = 0; i < 10; i++) {
        glVertex2f(i * 1.0, -10);
    }
    glEnd();
    glBegin(GL_POLYGON);
    glNormal3f(0.0f, 0.0f, 1.0f);
    float circleX = 0;
    float circleY = 20;
    constexpr float R = 5.0f;
    constexpr int n = 10000;
    for (int i = 0; i < n; ++i) {
        constexpr float Pi = 3.1415926535897932384626433832795f;
        glVertex2f(circleX + R * std::cos(2 * Pi / n * i), circleY + R * std::sin(2 * Pi / n * i));
    }
    glEnd();
}

/**
 * 展示函数
 */
void RenderScene() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 0.0);
    DrawMyObjects();
    glFlush();
}

/**
 * 主函数
 */
int main(int argc, char **argv) {
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600, 600);

    glutCreateWindow("My window");

    glClearColor(1, 1, 1, 0);

    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
    glutMainLoop();
    return 0;
}
